package ar.com.spsolutions.splilabrary.view.utils;

public class StringUtils {
	private StringUtils() {
		// reduce visibility
	}

	public static String getMethodName(final String methodName,
			final String... replacers) {
		String finalMethodName = methodName;
		for (int i = 0; i < replacers.length; i++) {
			finalMethodName = finalMethodName.replaceAll("/" + i + "/",
					replacers[i]);
		}
		return finalMethodName;
	}

	public static String upperSelecteds(final String string,
			final Integer... positions) {
		if ((positions != null) && (positions[0] < 1)) {
			return string;
		}
		final StringBuffer builder = new StringBuffer();
		int lastPosition = 0;
		if (positions != null) {
			for (final Integer position : positions) {
				if (position > lastPosition) {
					builder.append(string.substring(lastPosition, position - 1));
					builder.append(StringUtils
							.toUpperFunction(string, position));
					lastPosition = position;
				}
			}
		}
		builder.append(string.substring(lastPosition));
		return builder.toString();
	}

	private static char toUpperFunction(final String string,
			final Integer position) {
		return Character.toUpperCase(string.charAt(position - 1));
	}

	private static char toLowerFunction(final String string,
			final Integer position) {
		return Character.toLowerCase(string.charAt(position - 1));
	}

	public static String lowerSelecteds(final String string,
			final Integer... positions) {
		if ((positions != null) && (positions[0] < 1)) {
			return string;
		}
		final StringBuffer builder = new StringBuffer();
		int lastPosition = 0;
		if (positions != null) {
			for (final Integer position : positions) {
				if (position > lastPosition) {
					builder.append(string.substring(lastPosition, position - 1));
					builder.append(StringUtils
							.toLowerFunction(string, position));
					lastPosition = position;
				}
			}
		}
		builder.append(string.substring(lastPosition));
		return builder.toString();
	}

	public static String toGetter(final String property) {
		final String methodName = StringUtils.getMethodName("get/0/", property);
		return StringUtils.upperSelecteds(methodName, 4);
	}

	public static String toSetter(final String property) {
		final String methodName = StringUtils.getMethodName("set/0/", property);
		return StringUtils.upperSelecteds(methodName, 4);
	}

	public static String plural(final String string) {

		final int length = string.length() - 1;
		final Character lastCharacter = string.charAt(length);
		if (((Character) 'y').equals(lastCharacter)) {
			return string.subSequence(0, length) + "ies";
		}
		return string + "s";
	}

}
